package com.viking.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class GetImg {
        /** 设置获取验证码的Key值*/
   private String checkKey;
        {
                checkKey= String.valueOf(System.currentTimeMillis());
        }
    public String GetImg() throws Exception {

            String url = "http://viking.daxiangcoding.com/jeecg-boot/sys/randomImage/";
            url=url+checkKey;
            System.out.println("get的地址"+url);
            URL obj = new URL(url);
            HttpURLConnection con = (HttpURLConnection) obj.openConnection();
            con.setRequestMethod("GET");
            con.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
            con.setDoOutput(true);
            int responseCode = con.getResponseCode();
            System.out.println("Sending 'Get' request to URL : " + url);

            System.out.println("Response Code : " + responseCode);

            BufferedReader in = new BufferedReader(
                    new InputStreamReader(con.getInputStream()));
            String inputLine = null;

            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }

            in.close();
            System.out.println(response);
            String result1 = "";
            Matcher m = Pattern.compile("(?i)\"result\": *\"([^\"]*)\"").matcher(response.toString());
            if(m.find()) {
                    String result =m.group((1));
                    System.out.println(result);
                    String imgdata = result.replace("data:image/jpg;base64,","");
                    System.out.println(imgdata);
                    result1=imgdata;
            }
                return  result1;


    }

        public String getCheckKey() {
                return checkKey;
        }
}
